package com.gxks.lhs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxks.lhs.entity.Admin;
import com.gxks.lhs.mapper.AdminMapper;
import com.gxks.lhs.service.IAdminService;
//import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 管理员表 服务实现类
 * </p>
 *
 * @author LuoHaisong
 * @since 2023-05-18
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Override
    public boolean login(String username, String password) {
        LambdaQueryWrapper<Admin> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(Admin::getUsername, username).eq(Admin::getPassword, password);
        Admin admin1 = getOne(lambdaQuery);
        if (admin1 != null) {
            return true;
        }
        return false;
    }


    @Override
    public boolean update(Admin admin) {
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        String encode = null;
        if (admin.getPassword().trim() != null) {
//            encode = new BCryptPasswordEncoder().encode((admin.getEmail()));//密码加工
        }

        updateWrapper.lambda().eq(Admin::getId, admin.getId())
                .set(admin.getNickname() != null, Admin::getNickname, admin.getNickname())
                .set(admin.getPassword() != null, Admin::getPassword, encode)
                .set(admin.getEmail() != null, Admin::getEmail, admin.getEmail())//密码加工
                .set(admin.getAvatar() != null, Admin::getAvatar, admin.getAvatar());
        return update(updateWrapper);
    }

    @Override
    public Admin selectByUsername(String username) {
        LambdaQueryWrapper<Admin> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.eq(Admin::getUsername, username);
        Admin admin = getOne(lambdaQuery);
        if (admin != null) {
            return admin;
        }
        return null;
    }

    @Override
    public Admin selectById(String adminId) {
        return getById(adminId);
    }
}
